package com.qf.dao.impl;

import com.qf.dao.EmpDao;
import com.qf.entity.Emp;
import com.qf.util.DruidUtil;
import com.qf.util.Page;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/*
杨威
2021/9/16
19:13
*/
public class EmpDaoImpl implements EmpDao {
    QueryRunner queryRunner=new QueryRunner();
    @Override
    public Emp selectById(int id) {
        try {
            String sql="select * from emp where id=?";
            return queryRunner.query(DruidUtil.getConnection(),sql,new BeanHandler<>(Emp.class),id);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"查询单个员工出现异常");
        }
    }

    @Override
    public List<Emp> selectAll() {
        try {
            String sql="select * from emp";
            List<Emp> query = queryRunner.query(DruidUtil.getConnection(), sql, new BeanListHandler<>(Emp.class));
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"查询所有出现异常");
        }
    }

    @Override
    public List<Emp> selectAllByPage(Page page) {
        try {
            String sql="select * from emp limit ?,?";
            return queryRunner.query(DruidUtil.getConnection(),sql,new BeanListHandler<>(Emp.class),page.getStartRow(),page.getPageSize());
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"分页查询所有出现异常");
        }
    }

    @Override
    public Long empCount() {
        try {
            String sql="select count(*) from emp";
            Long query = queryRunner.query(DruidUtil.getConnection(), sql, new ScalarHandler<>());
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"查询数量出现异常");
        }
    }

    @Override
    public int insert(Emp emp) {
        try {
            String sql="insert into emp(name,salary,age) values (?,?,?);";
            Object[] params={emp.getName(),emp.getSalary(),emp.getAge()};
            return queryRunner.update(DruidUtil.getConnection(),sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"新增员工出现异常");
        }

    }

    @Override
    public int delete(Integer id) {
        try {
            String sql="delete from emp where id=?";
            return queryRunner.update(DruidUtil.getConnection(),sql,id);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"删除学生出现异常");
        }
    }

    @Override
    public int update(Emp emp) {
        try {
            String sql="update emp set name=?,salary=?,age=? where id=?";
            Object[] params={emp.getName(),emp.getSalary(),emp.getAge(),emp.getId()};
            return queryRunner.update(DruidUtil.getConnection(),sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"修改学生出现异常");
        }
    }
}
